package com.dex4d.ui.wallet.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.dex4d.R;
import com.dex4d.constants.IConstants;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.core.http.DefaultTransformer;
import com.dex4d.core.http.JsonApiWrapper;
import com.dex4d.core.subscribers.ProgressSubscriber;
import com.dex4d.glide.GlideUtil;
import com.dex4d.model.AssetModel;
import com.dex4d.model.HttpResult;
import com.dex4d.model.RequestParam;
import com.dex4d.model.User;
import com.dex4d.ui.main.model.TokenContract;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by chengyuchun on 2018/10/25.
 */
public class SelectTokenActivity extends AppToolBarBaseActivity {
    @BindView(R.id.rv_asset)
    RecyclerView mRvAsset;

    CommonAdapter mCommonAdapter;
    @Override
    protected void initToolbar() {
        showBackView();
        showTitle(getString(R.string.token_select));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_token;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void loadData() {
        //请求此接口获取token列表
        final RequestParam requestParam = new RequestParam();
        requestParam.setId(1);
        requestParam.setJsonrpc(IConstants.JSON_RPC_VERSION);
        requestParam.setMethod(IConstants.GATEWAY_GETTOKENTABLES);
        requestParam.setParams(new ArrayList<String>());

        JsonApiWrapper
                .serviceApi()
                .getTokenTables(requestParam)
                .compose(new DefaultTransformer<HttpResult<List<TokenContract>>>())
                .subscribe(new ProgressSubscriber<HttpResult<List<TokenContract>>>(this, true) {
                    @Override
                    public void onNext(HttpResult<List<TokenContract>> result) {
                        //获得Token列表
                        List<AssetModel> assetList = new ArrayList<AssetModel>();
                        for (TokenContract temp : result.getResult()) {
                            AssetModel assetModel = new AssetModel();
                            assetModel.address = User.getInstance().getAddress();
                            assetModel.symbol = temp.name;
                            assetModel.contracAddress = temp.token_addr;
                            assetModel.picUrl = temp.icon_app;
                            assetModel.balance = "0.0";
                            assetList.add(assetModel);
                        }

                        mCommonAdapter = new CommonAdapter<AssetModel>(SelectTokenActivity.this, R.layout.item_asset_token, assetList) {
                            @Override
                            protected void convert(ViewHolder holder, final AssetModel assetModel, int position) {
                                GlideUtil.displayImg((ImageView) holder.getView(R.id.img_token_logo),assetModel.picUrl);
                                holder.setText(R.id.tv_token_name, assetModel.symbol.toUpperCase());
                                holder.setOnClickListener(R.id.rl_token, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        EventBus.getDefault().post(assetModel);
                                        finish();
                                    }
                                });
                            }
                        };
                        mRvAsset.setLayoutManager(new LinearLayoutManager(SelectTokenActivity.this));
                        mRvAsset.setAdapter(mCommonAdapter);
                    }
                });

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //final List<AssetModel> assetList = CustomApplication.getDb().assetDao().getAll();
//                final List<AssetModel> assetList = MockData.getAssets();
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        mCommonAdapter = new CommonAdapter<AssetModel>(SelectTokenActivity.this, R.layout.item_asset_token, assetList) {
//                            @Override
//                            protected void convert(ViewHolder holder, final AssetModel assetModel, int position) {
//                                holder.setImageResource(R.id.img_token_logo,assetModel.localPic);
//                                holder.setText(R.id.tv_token_name, assetModel.symbol);
//                                //holder.setText(R.id.tv_balance, assetModel.balance);
//                                holder.setOnClickListener(R.id.rl_token, new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        EventBus.getDefault().post(assetModel);
//                                        finish();
//                                    }
//                                });
//                            }
//                        };
//                        mRvAsset.setLayoutManager(new LinearLayoutManager(SelectTokenActivity.this));
//                        mRvAsset.setAdapter(mCommonAdapter);
//                    }
//                });
//            }
//        }).start();
    }
}
